#include "pcr.h"
#include "rectype.h"
#include "util.h"
#include "nmfield.h"

#include "stdffield.h"
#include "stdffield.cpp"

using namespace std;
using namespace opsrc::stdf;

Pcr::Pcr()
{
}

Pcr::Pcr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex, endian e, char* buf )
    :RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e) {
    if (buf != 0)
        readFields(buf);
}

Pcr::~Pcr() {
}

const StdfField<int> Pcr::getHeadNum() const {
    return head_num;
}

void Pcr::setHeadNum(StdfField<int> headnum) {
    head_num = headnum;
}

const StdfField<int> Pcr::getSiteNum() const {
    return site_num;
}

void Pcr::setSiteNum(StdfField<int> sitenum) {
    site_num = sitenum;
}

const StdfField<long> Pcr::getPartCount() const {
    return part_cnt;
}

void Pcr::setPartCount(StdfField<long> partcnt) {
    part_cnt = partcnt;
}


const StdfField<long> Pcr::getRetestCount() const {
    return rtst_cnt;
}

void Pcr::setRetestCount(StdfField<long> retestcnt) {
    rtst_cnt = retestcnt;
}

const StdfField<long> Pcr::getAbortCount() const {
    return abrt_cnt;
}

void Pcr::setAbortCount(StdfField<long> abortcnt) {
    abrt_cnt = abortcnt;
}

const StdfField<long> Pcr::getGoodCount() const {
    return good_cnt;
}

void Pcr::setGoodCount(StdfField<long> goodcnt) {
    good_cnt = goodcnt;
}


const StdfField<long> Pcr::getFunctionalCount() const {
    return func_cnt;
}

void Pcr::setFunctionalCount(StdfField<long> functcnt) {
    func_cnt = functcnt;
}

void Pcr::readFields(char *buf) {

#if _DEBUG
    cout << "stdfrec= PCR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    int curpos = 0;
    // Read head number , 1 byte
    NmField<unsigned int, 1> headnum(buf+curpos, en);
    head_num.set(headnum.getData(), curpos, headnum.getByteCount(), headnum.getTotalByteCount());
    curpos += head_num.getTotalByteCount();
    // Read site num, 1 byte

    if (curpos < reclen -1) {
        NmField<unsigned int, 1> sitenum(buf+curpos, en);
        site_num.set(sitenum.getData(), curpos, sitenum.getByteCount(), sitenum.getTotalByteCount());
        curpos += site_num.getTotalByteCount();
    }

    // Read parts count, 4 bytes
    if (curpos >= reclen -1) {
        NmField<unsigned int, 4> partcnt(buf+curpos, en);
        part_cnt.set(partcnt.getData(), curpos, partcnt.getByteCount(), partcnt.getTotalByteCount());
        curpos += part_cnt.getTotalByteCount();
    }
    
    // Read retest count, 4 bytes
    if (curpos < reclen -1) {
        NmField<unsigned int, 4> rtstcnt(buf+curpos, en);
        rtst_cnt.set(rtstcnt.getData(), curpos, rtstcnt.getByteCount(), rtstcnt.getTotalByteCount());
        curpos += rtst_cnt.getTotalByteCount();
    }

    // Read abort count, 4 bytes
    if (curpos < reclen -1) {
        NmField<unsigned int, 4> abrtcnt(buf+curpos, en);
        abrt_cnt.set(abrtcnt.getData(), curpos, abrtcnt.getByteCount(), abrtcnt.getTotalByteCount());
        curpos += abrt_cnt.getTotalByteCount();
    }

    // Read good count, 4 bytes
    if (curpos < reclen -1) {
        NmField<unsigned int, 4> goodcnt(buf+curpos, en);
        good_cnt.set(goodcnt.getData(), curpos, goodcnt.getByteCount(), goodcnt.getTotalByteCount());
        curpos += good_cnt.getTotalByteCount();
    }

    // Read functional count, 4 bytes
    if (curpos < reclen -1) {
        NmField<unsigned int, 4> funccnt(buf+curpos, en);
        func_cnt.set(funccnt.getData(), curpos, funccnt.getByteCount(), funccnt.getTotalByteCount());
    }
}
